from PyQt5.QtCore import QSettings, QStandardPaths, QCoreApplication

class ConfigManager:
    def __init__(self, org_name, app_name, app_version):
        """
        初始化配置管理器
        :param org_name: 组织/公司名称
        :param app_name: 应用程序名称
        """
        QCoreApplication.setOrganizationName(org_name)
        QCoreApplication.setApplicationName(app_name)
        QCoreApplication.setApplicationVersion(app_version)
        self.settings = QSettings()

    def get(self, key, default=None):
        """
        读取配置值
        :param key: 配置键名
        :param default: 默认值（当键不存在时返回）
        :return: 配置值或默认值
        """
        return self.settings.value(key, default)

    def set(self, key, value):
        """
        设置配置值
        :param key: 配置键名
        :param value: 要存储的值（支持基本数据类型）
        """
        self.settings.setValue(key, value)
        self.settings.sync()  # 立即写入存储

    def remove(self, key):
        """
        删除指定配置项
        :param key: 要删除的配置键名
        """
        self.settings.remove(key)
        self.settings.sync()

    def clear_all(self):
        """
        清除所有配置项
        """
        self.settings.clear()
        self.settings.sync()

    def export_config(self, file_path):
        """
        导出当前配置到文件
        :param file_path: 导出文件路径
        """
        export_settings = QSettings(file_path, QSettings.IniFormat)
        for key in self.settings.allKeys():
            export_settings.setValue(key, self.settings.value(key))
        export_settings.sync()

    def import_config(self, file_path):
        """
        从文件导入配置
        :param file_path: 要导入的文件路径
        """
        import_settings = QSettings(file_path, QSettings.IniFormat)
        for key in import_settings.allKeys():
            self.set(key, import_settings.value(key))

    def get_all_keys(self):
        """
        获取所有配置键名
        :return: 键名列表
        """
        return self.settings.allKeys()

    def contains(self, key):
        """
        检查配置项是否存在
        :param key: 要检查的键名
        :return: bool
        """
        return self.settings.contains(key)

    @property
    def config_location(self):
        """
        获取当前配置文件存储路径
        :return: 文件路径字符串
        """
        return self.settings.fileName()

# # 使用示例
# if __name__ == "__main__":
#     # 需要先创建QCoreApplication实例
#     import sys
#     app = QCoreApplication(sys.argv)
#
#     # 初始化配置管理器
#     config = ConfigManager()
#
#     # 写入配置示例
#     config.set("window/size", "800x600")
#     config.set("user/name", "John Doe")
#     config.set("user/age", 30)
#     config.set("dark_mode", True)
#
#     # 读取配置示例
#     print("Window size:", config.get("window/size"))
#     print("Dark mode:", config.get("dark_mode", False))
#
#     # 导出配置示例
#     config.export_config("backup.ini")
#
#     # 导入配置示例
#     # config.import_config("backup.ini")
#
#     # 删除配置示例
#     config.remove("user/age")
#
#     # 获取所有键
#     print("All keys:", config.get_all_keys())
#
#     # 获取配置文件路径
#     print("Config location:", config.config_location)